﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BoolanElement.Storage;
using BoolanSlide.Document.Storage;
using BoolanSlide.FileManager;
using BoolanSlide.FileStruct;
using BoolanSlide.Language;
using Configuration;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

namespace BoolanElement
{
    public delegate Task<bool> AddSlide(BooSlide booSlide);
 
    public class SelectBPTTemplate
    {
        public bool IsShow
        {
            get
            {
                return selectTemplate.IsShow;
            }
            set
            {
                selectTemplate.IsShow = value;
            }
        }

        private const int ColumnNumber = 2;

        private SelectPanel selectTemplate;

        public AddSlide addSlide;

        public SelectBPTTemplate()
        {
            LanguageManager.LanguageChanged += LanguageManager_LanguageChanged;
        }

        void LanguageManager_LanguageChanged(object sender)
        {

            if (Configuration.FileAttributes.Ratio == BPTFileInformation.FileRatio.Ratio16_9)
            {
                selectTemplate.ChangeBkgImage(LanguageManager.InsertTemplate16_9);
            }
            else if (Configuration.FileAttributes.Ratio == BPTFileInformation.FileRatio.Ratio4_3)
            {
                selectTemplate.ChangeBkgImage(LanguageManager.InsertTemplate4_3);
            }
        }

        public void HidePanel()
        {
            if(selectTemplate != null)
                selectTemplate.IsShow = false;
        }

        public async Task<bool> LoadTemplate(string strPath)
        {
            Windows.ApplicationModel.Package package = Windows.ApplicationModel.Package.Current;
            Windows.Storage.StorageFolder installedLocation = package.InstalledLocation;
            StorageFolder file = await installedLocation.GetFolderAsync("Theme");

            StorageFile srcFile = await file.GetFileAsync(strPath);

            BooDocumentInMemory document = await BPTFile.OpenFile(srcFile);
            await document.CreateImageFile(await BooUtility.GetFolderFromDocumentFolderAsync(BooDocumentHelper.ImageTmpFolder));

            GlobalVariable.usingTemplate = await BooDocumentHelper.LoadFromMemory(document);

            await InitializePanel();

            return true;
        }

        public async Task<bool> AddFirstSlide()
        {
            return await addSlide(GlobalVariable.usingTemplate.Slides[0]);
        }

        private async Task<bool> InitializePanel()
        {
            double PanelWidth = 352;
            double PanelHeight = 357;
            double ItemWidth = 0;
            double ItemHeight = 0;
            string strBkgImagePath = null;

            Thickness PanelMargin = new Thickness(6, 45, 6, 17);
            Thickness ItemMargin = new Thickness(0, 0, 0, 0);

            Configuration.FileAttributes.Ratio = GlobalVariable.usingTemplate.FileInformation.Resolution;
            GlobalVariable.OpenedDocument.FileInformation.Resolution = GlobalVariable.usingTemplate.FileInformation.Resolution;

            if (Configuration.FileAttributes.Ratio == BPTFileInformation.FileRatio.Ratio16_9)
            {
                PanelWidth = 352;
                PanelHeight = 447;
                ItemWidth = UIMargin.TemplateItemWidth16_9;
                ItemHeight = UIMargin.TemplateItemHeight16_9;
                strBkgImagePath = LanguageManager.InsertTemplate16_9;
            }
            else if (Configuration.FileAttributes.Ratio == BPTFileInformation.FileRatio.Ratio4_3)
            {
                PanelWidth = 352;
                PanelHeight = 567;
                ItemWidth = UIMargin.TemplateItemWidth4_3;
                ItemHeight = UIMargin.TemplateItemHeight4_3;
                strBkgImagePath = LanguageManager.InsertTemplate4_3;
            }
            selectTemplate = new SelectPanel(PanelWidth, PanelHeight);
            selectTemplate.Initialize(4, ColumnNumber, ItemWidth, ItemHeight, PanelMargin, ItemMargin, strBkgImagePath);

            LostFocusObserver.Subscribe(this.HidePanel);

            foreach (BooSlide booSlide in GlobalVariable.usingTemplate.Slides)
            {
                Canvas templateCanvas = await booSlide.GetThumbNailsAtSize(new Size(ItemWidth, ItemHeight));
                templateCanvas.Tag = "TemplateSlide";
                selectTemplate.AddItem(templateCanvas, PressedNo);
            }

            Rect rect = Window.Current.Bounds;

            selectTemplate.SetPosition(0, rect.Height - PanelHeight-50);

            return true;
        }

        #region 选择模板
        private async void PressedNo(object sender, PointerRoutedEventArgs e)
        {
            await UILock.WaitSignal.WaitAsync();

            int columnNum = Grid.GetColumn((StackPanel)(sender));
            int rowNum = Grid.GetRow((StackPanel)(sender));

            int index = rowNum * ColumnNumber + columnNum;

            await addSlide(GlobalVariable.usingTemplate.Slides[index]);

            e.Handled = true;

            UILock.WaitSignal.Release();
        }
        #endregion 选择模板
    }
}
